import requests
import os
from ai.config import HEADERS, BASE_URL  # 从config模块导入统一配置

# 删除原有的headers和url定义
def upload_file(file_path):
    try:
        with open(file_path, 'rb') as f:
            import mimetypes
            file_name = os.path.basename(file_path)
            mime_type, _ = mimetypes.guess_type(file_name)
            mime_type = mime_type or 'application/octet-stream'
            
            files = {'file': (file_name, f, mime_type)}
            
            print(f"正在上传文件: {file_name} (MIME类型: {mime_type})")
            upload_headers = HEADERS.copy()
            upload_headers.pop('Content-Type', None)
            response = requests.post(BASE_URL + 'v1/files/', headers=upload_headers, files=files)
            response.raise_for_status()
            
            print("服务器响应:", response.text)
            return response.json()
    except requests.exceptions.RequestException as e:
        print(f"请求失败: {str(e)}")
        if hasattr(e, 'response') and e.response:
            print(f"响应状态码: {e.response.status_code}")
            print(f"响应内容: {e.response.text}")
        return None
    except Exception as e:
        print(f"未知错误: {str(e)}")
        import traceback
        print(f"错误堆栈:\n{traceback.format_exc()}")
        return None

if __name__ == '__main__':
    file_path = input("请输入要上传的文件路径: ").strip()
    if not os.path.exists(file_path):
        print("文件不存在")
        exit(1)
    
    result = upload_file(file_path)
    if result:
        print("\n文件上传成功！响应数据:")
        print(result)
    else:
        print("文件上传失败")